package Wires.presentation;


import java.io.File;
import java.util.Set;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.emf.common.ui.celleditor.ExtendedDialogCellEditor;
import org.eclipse.emf.ecore.*;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.emf.edit.ui.provider.PropertyDescriptor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
import org.eclipse.ui.dialogs.ListDialog;
import org.eclipse.ui.model.BaseWorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;

import Wires.presentation.FilePatternFilter;
import Wires.presentation.TextAndDialogCellEditor;
import Wires.impl.*;

public class CustomizedPropertyDescriptor extends PropertyDescriptor{

	public CustomizedPropertyDescriptor(Object object,
			IItemPropertyDescriptor itemPropertyDescriptor) {
		super(object, itemPropertyDescriptor);
		// TODO Auto-generated constructor stub
	}

	public CellEditor createPropertyEditor(Composite composite) {
			CellEditor result = null;  
			String attName = ((EStructuralFeature)itemPropertyDescriptor.getFeature(object)).getName();
			if (attName == "path") {
			   if ((this.object.getClass() == AtomicModelTransfomationTypeImpl.class)
			   	|| (this.object.getClass() == QueryTypeImpl.class)
			   	|| (this.object.getClass() == LibraryImpl.class)){
//				   result = new ExtendedDialogCellEditor(composite, getEditLabelProvider()){
//			             protected Object openDialogBox(Control cellEditorWindow) {
//			            	 	/*
//				            	FileDialog fd;
//				            	fd = new FileDialog(cellEditorWindow.getShell(), SWT.SAVE);
//				        		//fd.setFilterPath(Platform.getLocation().toOSString());
//				        		fd.setFilterNames(new String[] {"ATL files (*.atl)"});
//				        		fd.setFilterExtensions(new String[] { "*.atl"});
//				        		return fd.open();
//				        		*/
//
//				            	
//				        		ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(cellEditorWindow.getShell(), 
//				        				new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider() );
//				        		
//				        		dialog.setTitle("Select resource");
//				        		dialog.setMessage("Select ATL unit");
//				        		dialog.setInput(ResourcesPlugin.getWorkspace());
//				        		FilePatternFilter filter = new FilePatternFilter("atl"); 
//				        		dialog.addFilter(filter);
//				        		dialog.open();
//				        		if(dialog.getReturnCode()==dialog.OK){
//				        		    IFile result = (IFile) dialog.getResult()[0]; //Nos devuelve un IFile con el archivo seleccionado
//				        		    return result.getProject().getFullPath().toOSString() + File.separator + result.getProjectRelativePath().toOSString();
//				        		}else{
//				        			return "";
//				        		}
//			            }
//			         };
				   
				   result = new TextAndDialogCellEditor(composite){
					   protected Object openDialogBox(Control cellEditorWindow){
						   ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(cellEditorWindow.getShell(), 
			        				new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider() );
			        		
			        		dialog.setTitle("Select resource");
			        		dialog.setMessage("Select ATL unit");
			        		dialog.setInput(ResourcesPlugin.getWorkspace());
			        		FilePatternFilter filter = new FilePatternFilter("atl"); 
			        		dialog.addFilter(filter);
			        		dialog.open();
			        		if(dialog.getReturnCode()==dialog.OK){
			        		    IFile result = (IFile) dialog.getResult()[0]; //Nos devuelve un IFile con el archivo seleccionado
			        		    return result.getProject().getFullPath().toOSString() + File.separator + result.getProjectRelativePath().toOSString();
			        		}else{
			        			return null;
			        		}
					   }					  
				   };
			   }else if (this.object.getClass() == Model2TextTransformationTypeImpl.class){
				   result = new TextAndDialogCellEditor(composite){
					   protected Object openDialogBox(Control cellEditorWindow){
						   ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(cellEditorWindow.getShell(), 
			        				new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider() );
			        		
			        		dialog.setTitle("Select resource");
			        		dialog.setMessage("Select ecore");
			        		dialog.setInput(ResourcesPlugin.getWorkspace());
			        		FilePatternFilter filter = new FilePatternFilter("ecore"); 
			        		dialog.addFilter(filter);
			        		dialog.open();
			        		if(dialog.getReturnCode()==dialog.OK){
			        		    IFile result = (IFile) dialog.getResult()[0]; //Nos devuelve un IFile con el archivo seleccionado
			        		    return result.getProject().getFullPath().toOSString() + File.separator + result.getProjectRelativePath().toOSString();
			        		}else{
			        			return null;
			        		}
					   }					  
				   };
				   			   
			   }else if (this.object.getClass() == ModelTypeImpl.class){
				   
				   result = new TextAndDialogCellEditor(composite){
					   protected Object openDialogBox(Control cellEditorWindow){
						   ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(cellEditorWindow.getShell(), 
			        				new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider() );
			        		
			        		dialog.setTitle("Select resource");
			        		dialog.setMessage("Select metamodel");
			        		dialog.setInput(ResourcesPlugin.getWorkspace());
			        		dialog.open();
			        		if(dialog.getReturnCode()==dialog.OK){
			        		    IFile result = (IFile) dialog.getResult()[0]; //Nos devuelve un IFile con el archivo seleccionado
			        		    return result.getProject().getFullPath().toOSString() + File.separator + result.getProjectRelativePath().toOSString();
			        		}else{
			        			return null;
			        		}
					   }					  
				   };
				   
//						   result = new ExtendedDialogCellEditor(composite, getEditLabelProvider()){
//					             protected Object openDialogBox(Control cellEditorWindow) {
//					            	 ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(cellEditorWindow.getShell(), 
//						        				new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider() );
//						        		dialog.setTitle("Select resource");
//						        		dialog.setMessage("Select metamodel");
//						        		dialog.setInput(ResourcesPlugin.getWorkspace());
//						        		FilePatternFilter filter = new FilePatternFilter("ecore"); 
//						        		dialog.addFilter(filter);
//						        		dialog.open();
//						        		if(dialog.getReturnCode()==dialog.OK){
//						        		    IFile result = (IFile) dialog.getResult()[0]; //Nos devuelve un IFile con el archivo seleccionado
//						        		    return result.getProject().getFullPath().toOSString() + File.separator + result.getProjectRelativePath().toOSString();
//						        		}else{
//						        			return "";
//						        		}
//					            }
//					         };
				   
			   } else if ((this.object.getClass() == ModelImpl.class) || (this.object.getClass() == BasicDataImpl.class)){
				   result = new TextAndDialogCellEditor(composite){
					   protected Object openDialogBox(Control cellEditorWindow){
						   ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(cellEditorWindow.getShell(), 
			        				new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider() );
			        		
			        		dialog.setTitle("Select resource");
			        		dialog.setMessage("Select model");
			        		dialog.setInput(ResourcesPlugin.getWorkspace());
			        		dialog.open();
			        		if(dialog.getReturnCode()==dialog.OK){
			        		    IFile result = (IFile) dialog.getResult()[0]; //Nos devuelve un IFile con el archivo seleccionado
			        		    return result.getProject().getFullPath().toOSString() + File.separator + result.getProjectRelativePath().toOSString();
			        		}else{
			        			return null;
			        		}
					   }					  
				   };
				   
//				   result = new ExtendedDialogCellEditor(composite, getEditLabelProvider()){
//			             protected Object openDialogBox(Control cellEditorWindow) {		
//			            	 ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(cellEditorWindow.getShell(), 
//				        				new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider() );
//				        		dialog.setTitle("Select resource");
//				        		dialog.setMessage("Select model");
//				        		dialog.setInput(ResourcesPlugin.getWorkspace());
//				        		dialog.open();
//				        		if(dialog.getReturnCode()==dialog.OK){
//				        		    IFile result = (IFile) dialog.getResult()[0]; //Nos devuelve un IFile con el archivo seleccionado
//				        		    return result.getProject().getFullPath().toOSString() + File.separator  + result.getProjectRelativePath().toOSString();
//				        		}else{
//				        			return "";
//				        		}
//			            }
//			         };
			   }
		   } else if(attName == "uri"){
			   
			   result = new TextAndDialogCellEditor(composite){
				   protected Object openDialogBox(Control cellEditorWindow) {
		        		EPackage.Registry registro=EPackage.Registry.INSTANCE;
		        		Set<String> uris=registro.keySet();
		        		ListDialog dialogo=new ListDialog(cellEditorWindow.getShell());
		        		dialogo.setAddCancelButton(true);
		        		dialogo.setLabelProvider(new LabelProvider());
		        		dialogo.setContentProvider(new ArrayContentProvider());
		        		dialogo.setInput(uris);
		        		dialogo.setTitle("Select metamodel by URI");
		        		dialogo.open();
		        		String result;
		        		if (dialogo.getResult() == null){ 
		        			result = "";
		        		}else{
		        			result = (String) dialogo.getResult()[0];
		        		}
		        		return result;	
				   }
			   };
			   
//			   result = new ExtendedDialogCellEditor(composite, getEditLabelProvider()){
//		             protected Object openDialogBox(Control cellEditorWindow) {
//		        		EPackage.Registry registro=EPackage.Registry.INSTANCE;
//		        		Set<String> uris=registro.keySet();
//		        		ListDialog dialogo=new ListDialog(cellEditorWindow.getShell());
//		        		dialogo.setAddCancelButton(true);
//		        		dialogo.setLabelProvider(new LabelProvider());
//		        		dialogo.setContentProvider(new ArrayContentProvider());
//		        		dialogo.setInput(uris);
//		        		dialogo.setTitle("Select metamodel by URI");
//		        		dialogo.open();
//		        		String result;
//		        		if (dialogo.getResult() == null){ 
//		        			result = "";
//		        		}else{
//		        			result = (String) dialogo.getResult()[0];
//		        		}
//		        		return result;
//		            }
//		         };
		   }else{
			   result = super.createPropertyEditor(composite);
		   }
		   return result;
		}
}


//VERSION ANTIGUA
//public class CustomizedPropertyDescriptor extends PropertyDescriptor{
//
//	public CustomizedPropertyDescriptor(Object object,
//			IItemPropertyDescriptor itemPropertyDescriptor) {
//		super(object, itemPropertyDescriptor);
//		// TODO Auto-generated constructor stub
//	}
//
//	public CellEditor createPropertyEditor(Composite composite) {
//			CellEditor result = null;  
//			String attName = ((EStructuralFeature)itemPropertyDescriptor.getFeature(object)).getName();
//			if (attName == "path") {
//			   if ((this.object.getClass() == AtomicModelTransfomationTypeImpl.class)
//			   	|| (this.object.getClass() == QueryTypeImpl.class)
//			   	|| (this.object.getClass() == LibraryImpl.class)){
//				   result = new ExtendedDialogCellEditor(composite, getEditLabelProvider()){
//			             protected Object openDialogBox(Control cellEditorWindow) {
//			            	 	/*
//				            	FileDialog fd;
//				            	fd = new FileDialog(cellEditorWindow.getShell(), SWT.SAVE);
//				        		//fd.setFilterPath(Platform.getLocation().toOSString());
//				        		fd.setFilterNames(new String[] {"ATL files (*.atl)"});
//				        		fd.setFilterExtensions(new String[] { "*.atl"});
//				        		return fd.open();
//				        		*/
//				        		ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(cellEditorWindow.getShell(), 
//				        				new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider() );
//				        		dialog.setTitle("Select resource");
//				        		dialog.setMessage("Select ATL unit");
//				        		dialog.setInput(ResourcesPlugin.getWorkspace());
//				        		
//				        		FilePatternFilter filter = new FilePatternFilter("atl"); 
//				        		dialog.addFilter(filter);
//				        		dialog.open();
//				        		if(dialog.getReturnCode()==dialog.OK){
//				        		    IFile result = (IFile) dialog.getResult()[0]; //Nos devuelve un IFile con el archivo seleccionado
//				        		    return result.getProject().getFullPath().toOSString() + File.separator + result.getProjectRelativePath().toOSString();
//				        		}else{
//				        			return "";
//				        		} 
//			            }
//			         };
//			   }else if (this.object.getClass() == ModelTypeImpl.class){
//						   result = new ExtendedDialogCellEditor(composite, getEditLabelProvider()){
//					             protected Object openDialogBox(Control cellEditorWindow) {
//					            	 ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(cellEditorWindow.getShell(), 
//						        				new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider() );
//						        		dialog.setTitle("Select resource");
//						        		dialog.setMessage("Select metamodel");
//						        		dialog.setInput(ResourcesPlugin.getWorkspace());
//						        		FilePatternFilter filter = new FilePatternFilter("ecore"); 
//						        		dialog.addFilter(filter);
//						        		dialog.open();
//						        		if(dialog.getReturnCode()==dialog.OK){
//						        		    IFile result = (IFile) dialog.getResult()[0]; //Nos devuelve un IFile con el archivo seleccionado
//						        		    return result.getProject().getFullPath().toOSString() + File.separator + result.getProjectRelativePath().toOSString();
//						        		}else{
//						        			return "";
//						        		}
//					            }
//					         };
//			   } else if ((this.object.getClass() == ModelImpl.class) || (this.object.getClass() == BasicDataImpl.class)){
//				   result = new ExtendedDialogCellEditor(composite, getEditLabelProvider()){
//			             protected Object openDialogBox(Control cellEditorWindow) {
//			            	 ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(cellEditorWindow.getShell(), 
//				        				new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider() );
//				        		dialog.setTitle("Select resource");
//				        		dialog.setMessage("Select model");
//				        		dialog.setInput(ResourcesPlugin.getWorkspace());
//				        		dialog.open();
//				        		if(dialog.getReturnCode()==dialog.OK){
//				        		    IFile result = (IFile) dialog.getResult()[0]; //Nos devuelve un IFile con el archivo seleccionado
//				        		    return result.getProject().getFullPath().toOSString() + File.separator + result.getProjectRelativePath().toOSString();
//				        		}else{
//				        			return "";
//				        		}
//			            }
//			         };
//			   }
//		   } else if(attName == "uri"){
//			   result = new ExtendedDialogCellEditor(composite, getEditLabelProvider()){
//		             protected Object openDialogBox(Control cellEditorWindow) {
//		        		EPackage.Registry registro=EPackage.Registry.INSTANCE;
//		        		Set<String> uris=registro.keySet();
//		        		ListDialog dialogo=new ListDialog(cellEditorWindow.getShell());
//		        		dialogo.setAddCancelButton(true);
//		        		dialogo.setLabelProvider(new LabelProvider());
//		        		dialogo.setContentProvider(new ArrayContentProvider());
//		        		dialogo.setInput(uris);
//		        		dialogo.setTitle("Select metamodel by URI");
//		        		dialogo.open();
//		        		String result;
//		        		if (dialogo.getResult() == null){ 
//		        			result = "";
//		        		}else{
//		        			result = (String) dialogo.getResult()[0];
//		        		}
//		        		return result;
//		            }
//		         };
//		   }else{
//			   result = super.createPropertyEditor(composite);
//		   }
//		   return result;
//		}
//}

